//
//  EmoticonManager.swift
//  表情键盘
//
//  Created by 梁智 on 15/12/2.
//  Copyright © 2015年 lzfree. All rights reserved.
//

import Foundation

class EmoticonManager {
    
    /// 单例
    static let sharedManager = EmoticonManager()
    
    // 表情包数组
    lazy var packages = [EmoticonPackage]()
    
    // MARK: - 构造函数
    private init() {
        
        loadPlist()

    }
    
    /// 添加最近表情符号
    func addFavorite(em: Emoticon) {
        em.times++
        
        if !packages[0].emoticons.contains(em) {
            packages[0].emoticons.insert(em, atIndex: 0)
            // 删除倒数第二个表情
            packages[0].emoticons.removeAtIndex(packages[0].emoticons.count - 2)
        }
        
        // 排序
        packages[0].emoticons.sortInPlace { (em1, em2) -> Bool in
            em1.times > em2.times
        }
        
        //print(packages[0].emoticons as NSArray)
    }
    
    /// 从 emoticons.plist 加载表情包数据
    private func loadPlist() {
        // 0. 添加最近分组
        packages.append(EmoticonPackage(dict: ["group_name_cn": "最近"]))
        
        // 1. emoticons.plist 路径
        guard let path = NSBundle.mainBundle().pathForResource("emoticons", ofType: "plist", inDirectory: "Emoticons.bundle") else {
            print("emoticons 文件不存在")
            return
        }
        
        // 2. 加载字典
        guard let dict = NSDictionary(contentsOfFile: path) else {
            print("数据加载错误")
            return
        }
        
        // 3. 提取 packages 中的 id 字符串对应的数组
        let array = (dict["packages"] as! NSArray).valueForKey("id") as! [String]
        
        for id in array {
            loadInfoPlist(id)
        }
    }
    
    /// 加载 id 目录下的 info.plist 文件
    private func loadInfoPlist(id: String) {
        
        // info.plist 路径
        let path = NSBundle.mainBundle().pathForResource("info", ofType: "plist", inDirectory: "Emoticons.bundle/\(id)")!
        
        // 2. 加载字典
        let dict = NSDictionary(contentsOfFile: path) as! [String: AnyObject]
        
        // 3. 字典转模型
        packages.append(EmoticonPackage(dict: dict))
    }
}